package com.example.ucsur;


import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnInfoWindowClickListener;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapSearchActivity extends Activity {
	double lat;
	double lng;
	LatLng position;
	String address;
	String sup;
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_thumb1);
        Bundle extra = getIntent().getExtras();
        final String data = new String(extra.getString("dataFromActivity"));
		address = new String(extra.getString("address"));
		Thread t = new Thread(new Runnable() {
			public void run() {
				getLatLongFromAddress(address);
			}
		});
		t.start();
		while(t.isAlive() == true)
		{
		}
		position = new LatLng(lat, lng);
        GoogleMap map = ((MapFragment) getFragmentManager()
                .findFragmentById(R.id.map)).getMap();
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(position, 18));
        Marker marker = map.addMarker(new MarkerOptions()
        .title(address)
        .position(position));
        map.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
        	@Override
        	public void onInfoWindowClick(Marker marker) {
				Intent intent = new Intent(getBaseContext(), ThumbActivity.class);
				intent.putExtra("address", address);
				intent.putExtra("dataFromActivity", data);
				startActivity(intent);
        	}
        });

      
        // Get a handle to the Map Fragment

    }    
    public void getLatLongFromAddress(String address) {

        String uri = "http://maps.google.com/maps/api/geocode/json?address=" +
                      address + "&sensor=false";
        HttpGet httpGet = new HttpGet(uri);
        HttpClient client = new DefaultHttpClient();
        HttpResponse response;
        StringBuilder stringBuilder = new StringBuilder();

        try {
            response = client.execute(httpGet);
            HttpEntity entity = response.getEntity();
            InputStream stream = entity.getContent();
            int b;
            while ((b = stream.read()) != -1) {
                stringBuilder.append((char) b);
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject = new JSONObject(stringBuilder.toString());
            sup = new String(jsonObject.toString());

            lng = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
                .getJSONObject("geometry").getJSONObject("location")
                .getDouble("lng");

            lat = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
                .getJSONObject("geometry").getJSONObject("location")
                .getDouble("lat");

        } catch (JSONException e) {
            e.printStackTrace();
        }

    }
    }



